#!/usr/bin/python

import os
from subprocess import run
from absl import app, flags

flags.DEFINE_string('command', None, 'command to run.')
flags.DEFINE_string('work_dir', None, 'change work dir if not None.')
flags.DEFINE_string('channel', None, 'channel message send to.')

def change_work_dir():
  if flags.FLAGS.work_dir == None:
    return
  
  os.chdir(flags.FLAGS.work_dir)

def run_command(command: str):
  data = run(command, capture_output=True, shell=True)

  return (data.stdout + data.stderr).decode()

def send_slack_noti(message: str):
  cmd = f'noti -s {flags.FLAGS.channel} "{message}"'
  
  run_command(cmd)

def main(_):
  change_work_dir()

  output = run_command(flags.FLAGS.command)

  send_slack_noti(output)

if __name__ == "__main__":
  app.run(main)
